﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace FYPHP
{
    public partial class AdminForProductUpload : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
        int p_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdItem;
                SqlDataReader dtrItem;
                conn.Open();

                cmdItem = new SqlCommand("Select * From Products where p_status = 'Pending'", conn);
                dtrItem = cmdItem.ExecuteReader();


                ItemList.DataSource = dtrItem;
                ItemList.DataBind();
                

                dtrItem.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {

            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdUser;
                conn.Open();

                cmdUser = new SqlCommand("Update Products Set p_status = @Status where p_id=@Id ", conn);
                cmdUser.Parameters.AddWithValue("@Status", "Approve");
                cmdUser.Parameters.AddWithValue("@Id", lblPID.Text);
                cmdUser.ExecuteNonQuery();


                conn.Close();


                MultiView2.ActiveViewIndex = -1;
                Response.Redirect("AdminForProductUpload.aspx");
            }
            catch (SqlException ex)
            {

            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdUser;
                conn.Open();

                cmdUser = new SqlCommand("Update Products Set p_status = @Status where p_id=@Id ", conn);
                cmdUser.Parameters.AddWithValue("@Status", "Reject");
                cmdUser.Parameters.AddWithValue("@Id", lblPID.Text);
                cmdUser.ExecuteNonQuery();


                conn.Close();


                MultiView2.ActiveViewIndex = -1;
                Response.Redirect("AdminForProductUpload.aspx");
            }
            catch (SqlException ex)
            {

            }
        }


        protected void rptAds_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "ViewProduct")
            {
                MultiView2.ActiveViewIndex = 0;
                p_id = Convert.ToInt32(e.CommandArgument.ToString());

                try
                {
                    SqlCommand cmdSearch;
                    SqlDataReader dtrSearch;
                    conn.Open();

                    cmdSearch = new SqlCommand("SELECT * FROM Products p, Users u WHERE p.u_id=u.u_id AND p.p_id=@id", conn);
                    cmdSearch.Parameters.AddWithValue("@id", p_id);
                    dtrSearch = cmdSearch.ExecuteReader();

                    dtrSearch.Read();

                    lblPID.Text = dtrSearch["p_id"].ToString();
                    lblSeller.Text = dtrSearch["username"].ToString();
                    lblProductName.Text = dtrSearch["p_name"].ToString();
                    lblDesc.Text = dtrSearch["p_desc"].ToString();
                    lblCat.Text = dtrSearch["p_category"].ToString();
                    lblQuantity.Text = dtrSearch["p_quantity"].ToString();
                    lblPrice.Text = dtrSearch["p_price"].ToString();
                    
                    dtrSearch.Close();
                    conn.Close();
                }
                catch (SqlException ex)
                {

                }


            }

        }


        public string getPic()
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select p_picture From Products Where p_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", p_id);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            String pic = dtrSearch["p_picture"].ToString();

            conn.Close();

            return pic;

        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }



    }
}